package com.yunhe.policyfulfillment.authority.service.impl;

import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.policyfulfillment.authority.domain.PolicyMenu;
import com.yunhe.policyfulfillment.authority.domain.PolicyRole;
import com.yunhe.policyfulfillment.authority.repository.PolicyMenuRepository;
import com.yunhe.policyfulfillment.authority.service.PolicyMenuService;
import com.yunhe.policyfulfillment.authority.service.PolicyRoleAndMenuRelationService;
import com.yunhe.policyfulfillment.authority.service.PolicyRoleService;
import lombok.extern.slf4j.Slf4j;
import com.yunhe.common.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhengbo
 * @date 2021/9/17 17:25
 */
@Service
@Slf4j
public class PolicyMenuServiceImpl implements PolicyMenuService {

    @Autowired
    private PolicyMenuRepository policyMenuRepository;

    @Autowired
    private PolicyRoleAndMenuRelationService policyRoleAndMenuRelationService;

    @Autowired
    private PolicyRoleService policyRoleService;

    @Override
    public int createMenuSelective(PolicyMenu menu) throws UniqueConstraintsException {
        PolicyMenu menuByTitle = policyMenuRepository.searchMenuByTitle(menu.getTitle());
        if (menuByTitle != null) {
            throw new UniqueConstraintsException("此菜单名已存在");
        }
        if (null != menu.getParentId()) {
            PolicyMenu parentPolicyMenu = policyMenuRepository.searchMenuByPrimaryKey(menu.getParentId());
            menu.setLevel(parentPolicyMenu.getLevel() + 1);
        } else {
            menu.setLevel(1L);
        }
        menu.setSn(String.valueOf(System.currentTimeMillis()));
        return policyMenuRepository.createMenuSelective(menu);
    }

    @Override
    public int deleteMenuByPrimaryKey(Long id) {
        //同步删除关系表
        policyRoleAndMenuRelationService.deletePolicyMenusByMenuId(id);
        return policyMenuRepository.deleteMenuByPrimaryKey(id);
    }

    @Override
    public void deleteMenuByIds(String ids) {
        String[] idsStr = ids.split(",");
        for (String id : idsStr) {
            deleteMenuByPrimaryKey(Long.valueOf(id));
        }
    }

    @Override
    public int updateMenuByPrimaryKeySelective(PolicyMenu menu) throws UniqueConstraintsException {
        PolicyMenu menuByTitle = policyMenuRepository.searchMenuByTitle(menu.getTitle());
        if (menuByTitle != null && !menu.getId().equals(menuByTitle.getId())) {
            throw new UniqueConstraintsException("此菜单名已存在");
        }
        if (null != menu.getParentId()) {
            PolicyMenu parentPolicyMenu = policyMenuRepository.searchMenuByPrimaryKey(menu.getParentId());
            menu.setLevel(parentPolicyMenu.getLevel() + 1);
        } else {
            menu.setLevel(1L);
        }
        return policyMenuRepository.updateMenuByPrimaryKeySelective(menu);
    }

    @Override
    public PolicyMenu searchMenuByPrimaryKey(Long id) {
        return policyMenuRepository.searchMenuByPrimaryKey(id);
    }

    @Override
    public List<PolicyMenu> searchPolicyMenusByRoleId(Long roleId) {
        List<PolicyMenu> menus = policyRoleAndMenuRelationService.searchPolicyMenusByRoleId(roleId);
        List<PolicyMenu> allPolicyMenuList = policyMenuRepository.searchPolicyMenus();
        allPolicyMenuList.forEach(menu -> {
            if (menus.contains(menu)) {
                menu.setEnable(true);
            }
        });
        return setChildren(allPolicyMenuList);
    }

    @Override
    public List<PolicyMenu> searchPolicyMenus() {
        List<PolicyMenu> policyMenuList = policyMenuRepository.searchPolicyMenus();
        return setChildren(policyMenuList);
    }

    @Override
    public List<PolicyMenu> searchPolicyMenuListByRoleIdForLogin(Long roleId) {
        List<PolicyMenu> menus;
        PolicyRole policyRole = policyRoleService.searchPolicyRoleByPrimaryKey(roleId);
        if ("Admin".equals(policyRole.getRoleCode())) {
            menus = searchPolicyMenus();
        } else {
            menus = policyRoleAndMenuRelationService.searchPolicyMenusByRoleId(roleId);
        }
        return setChildren(menus);
    }

    //设置子集
    private List<PolicyMenu> setChildren(List<PolicyMenu> menuList) {
        if (CollectionUtil.isNotEmpty(menuList)) {
            for (PolicyMenu parentMenu : menuList) {
                for (PolicyMenu childrenMenu : menuList) {
                    if (childrenMenu.getParentId() != null &&
                            parentMenu.getId().equals(childrenMenu.getParentId())) {
                        if (CollectionUtil.isEmpty(parentMenu.getChildren())) {
                            ArrayList<PolicyMenu> menus = new ArrayList<>();
                            menus.add(childrenMenu);
                            parentMenu.setChildren(menus);
                        } else {
                            parentMenu.getChildren().add(childrenMenu);
                        }
                    }
                }
            }
        }
        menuList.removeIf(menu -> menu.getParentId() != null);
        return menuList;
    }
}
